﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;


namespace SdmxMl.SmsImportCfg
{
    [TypeConverter(typeof(GroupCfg.ExpressionConverter))]
    public class GroupCfg
    {
        #region Internal TypeConverter
        public class ExpressionConverter : TypeConverter
        {

            public override bool GetPropertiesSupported(ITypeDescriptorContext context)
            {
                return true;
            }

            public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context,
                object value, Attribute[] attributes)
            {
                PropertyDescriptorCollection properties =
                    TypeDescriptor.GetProperties(value, attributes);

                // Changing the display order
                string[] textOrder = new string[properties.Count];
                textOrder[0] = "GroupKey";
                textOrder[1] = "ColKey";
                // Return a sorted list of properties
                return properties.Sort(textOrder);
            }
        }
        #endregion //Internal TypeConverter


        #region Properties

        private string _keyGroup;
        [DefaultValue("")]
        [Category("Activation")]
        [RefreshProperties(RefreshProperties.Repaint)]
        [DescriptionAttribute("Defines the Group name to use in the Excel Worksheet when many Artefacts are present at the same time. " )]
        public string GroupKey
        {
            get { return _keyGroup; }
            set { _keyGroup = value; }
        }

        private int _colKey;
        [DefaultValue(0)]
        [Category("Activation")]
        [RefreshProperties(RefreshProperties.Repaint)]
        [DescriptionAttribute("Defines the column index in the Excel Worksheet where the Group key is given." +
            "This information is only required when GroupKey is defined (not empty).")]
        public int ColKey
        {
            get { return _colKey; }
            set 
            { 
                _colKey = value;
                if (_colKey <= 0)
                {
                    _colKey = 0;
                    GroupKey = string.Empty;
                }
            }
        }

        #endregion // Properties

        #region Construction

        public GroupCfg()
        {
            _keyGroup = string.Empty;
            _colKey = 0;

        }

        #endregion //Construction

        #region String Representation

        public override string ToString()
        {
            return IsUsed ? "used" : "Unused";
        }

        public bool IsUsed
        {
            get
            {
                return !(_colKey == 0 || string.IsNullOrEmpty(_keyGroup));
            }
        }

        #endregion
    }
}
